﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Extensions;
using Xugege.AbpExtension.Com.Dto;

namespace Xugege.AbpExtension.Com.Helper
{
    public class TreeListGenerater
    {
        private const string CodeRule = "1-2-2";
        private const char CodeRuleSplit = '-';

        /// <summary>
        /// 根据代码规则 生成代码树
        /// </summary>
        /// <param name="list"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        public static IList<DataWithCodeDto> Generate(IList<DataWithCodeDto> list, string rule = CodeRule)
        {

            IList<string> ruleSplits = rule.Split(CodeRuleSplit).ToList();
            if (ruleSplits.Any(m => !IsNumber(m)))
            {
                throw new Exception("代码规则设置不正确");
            }
            IList<int> ruleSplitsInt = ruleSplits.Select(int.Parse).ToList();
            IList<int> levelLengths = new List<int>();
            for (int i = 0; i < ruleSplitsInt.Count; i++)
            {
                var level = ruleSplitsInt[i];
                for (int j = 0; j < i; j++)
                {
                    var oldLevel = ruleSplitsInt[j];
                    level += oldLevel;
                }
                levelLengths.Add(level);
            }
            foreach (var dto in list)
            {
                int levelOrder = levelLengths.IndexOf(dto.Code.Length);
                if (levelOrder == 0)
                {
                    dto.ParentCode = null;
                }
                else
                {
                    var parentLevelLength = dto.Code.Length - ruleSplitsInt[levelOrder];
                    dto.ParentCode = dto.Code.Substring(0, parentLevelLength);
                    var parent = list.FirstOrDefault(m => m.Code.Length == parentLevelLength && m.Code == dto.ParentCode);
                    if (parent == null)
                    {
                        //如果没有找到对应的父节点 自身作为父节点
                        dto.ParentCode = null;
                    }
                }
            }
            return list;
        }

        
        public static void Generate<T>(ref IList<JsTreeDto<T>> list, string rule = CodeRule) where T : class, new()
        {
            IList<string> ruleSplits = rule.Split(CodeRuleSplit).ToList();
            if (ruleSplits.Any(m => !IsNumber(m)))
            {
                throw new Exception("代码规则设置不正确");
            }
            IList<int> ruleSplitsInt = ruleSplits.Select(int.Parse).ToList();
            IList<int> levelLengths = new List<int>();
            for (int i = 0; i < ruleSplitsInt.Count; i++)
            {
                var level = ruleSplitsInt[i];
                for (int j = 0; j < i; j++)
                {
                    var oldLevel = ruleSplitsInt[j];
                    level += oldLevel;
                }
                levelLengths.Add(level);
            }
            foreach (var dto in list)
            {
                int levelOrder = levelLengths.IndexOf(dto.Id.Length);
                if (levelOrder == 0)
                {
                    dto.Parent = null;
                }
                else
                {
                    var parentLevelLength = dto.Id.Length - ruleSplitsInt[levelOrder];
                    dto.Parent = dto.Id.Substring(0, parentLevelLength);
                    var parent = list.FirstOrDefault(m => m.Id.Length == parentLevelLength && m.Id == dto.Parent);
                    if (parent == null)
                    {
                        //如果没有找到对应的父节点 自身作为父节点
                        dto.Parent = null;
                    }
                    else
                    {
                        var brothers = parent.Children ?? new List<JsTreeDto<T>>();
                        brothers.Add(dto);
                    }
                }
            }
        }

        private static bool IsNumber(string source)
        {
            int target = 0;
            return int.TryParse(source, out target);
        }

    }
}
